# -*- coding:utf-8 -*-

from sqlalchemy import create_engine
from sqlalchemy.orm import declarative_base
from sqlalchemy import Column, String
from sqlalchemy.orm import sessionmaker
from Slave.config.slave_config import *
from logger_config import logger_factory
from role_config import role


engin = None
DBSession = None
Base = declarative_base()
engine = create_engine(DATABASE_URI, echo=True)
DBSession = sessionmaker(bind=engine)


class TransferDao(Base):
    __table_name__ = 'MFGRPA_TB_IUI_TRANSFER_LOT_AUTO_RECEIVE_HIST'
    __table_args__ = {'sqlite_autoincrement': True}

    lot_id = Column('LOTID', String(100), primary_key=True)
    prod_id = Column('PRODID', String(100))


def create_table():
    Base.metadata.create_all(engine)


def delete_table(table):
    table.__table__.drop(engine)


def insert_lot(lot):
    session = DBSession()
    session.add(lot)
    session.commit()
    session.close()


def delete_lot(table, lotid:str):
    session = DBSession()
    delete_num = session.query(table).filter(table.lotid==lotid).delete()
    session.commit()
    session.close()


def delete_all_lot(table):
    session = DBSession()
    session.query(table).delete()
    session.commit()
    session.close()


